package leetcode10;

import java.util.ArrayList;
import java.util.List;

public class test51 {
    /**
     * 46. 全排列
     * @param nums
     * @return
     */
    //DFS
    public List<List<Integer>> permute(int[] nums) {
        int len = nums.length;
        List<List<Integer>> list = new ArrayList<>();
        if(len == 0){
            return list;
        }
        List<Integer> path = new ArrayList<>();
        boolean[] user = new boolean[len];
        dfs(nums,len,0,path,user,list);
        return list;
    }
    public void dfs(int[] nums,int len,int depth,List<Integer> path,boolean[] user,List<List<Integer>> list){
        if(depth == len){
            list.add(new ArrayList<>(path));
        }
        for(int i = 0;i < len;i++){
            if(!user[i]){
                user[i] = true;
                path.add(nums[i]);
                dfs(nums,len,depth + 1,path,user,list);
                user[i] = false;
                path.removeLast();
            }
        }
    }
    //
}
